home *** CD-ROM | disk | FTP | other *** search
- @echo off
- ! existVar.bat Batch file to check/ensure that a variable exists
- !
- ! existVar name default
- !
- ! where 'name' is the name of the variable and default is the value to
- ! which the variable should be set if it does not exists.
- ! This batch is useful because MacDOS (like DOS) removes a user defined
- ! variable when it gets assigned the empty string. As a consequence, a
- ! check like if %var% == "whatever" goto LBL generates an error when
- ! the variable does not exist.
- !
- ! The simplest way to check for existence of a variable is as follows:
- ! if not "%varName%" == %%VARNAME%% goto EXISTS_LBL
- ! This check works because a reference to a non-existing variable returns
- ! a capitalised version of the reference itself. So, if the variable varName
- ! does not exist, an attempt to de-reference it returns the string %VARNAME% .
- ! The double percent signs are necessary to represent one '%'.
- !
- ! Unfortunately, you cannot derefrence twice a variable. That is, you cannot
- ! extract the value from a variable whose name is stored in another variable.
- ! Therefore, the simple 'if' shown above cannot be used in a batch like this
- ! in which the name of a variable is passed as an argument. The way to do it
- ! here is to use the internal checking of MacDOS.
- !
- ! This batch files lets you assign a default to a variable and it ensures
- ! that the variable exists. This of course is only true if the second
- ! parameter is defined!
- !
-
- !
- ! append a character to the variable and then remove it. MacDOS will
- ! reports error 63 if the variable does not exist. To keep it simple,
- ! we assume that the variable does not exist if ANY error occurs.
- !
- onerror VAR_ERR_LBL
- incr %1 by "*"
- decr %1
- goto DONE_LBL
-
- :VAR_ERR_LBL
- !
- ! The variable does not exist. Create it by setting it to the requested default.
- set %1=%2
-
- :DONE_LBL
-